package com.livepan.server.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.livepan.server.dao.${Domain}DOMapper;
import com.livepan.server.dataobject.${Domain}DO;
import com.livepan.server.dataobject.${Domain}DOExample;
import com.livepan.server.error.BusinessException;
import com.livepan.server.error.EmBusinessError;
import com.livepan.server.service.model.${Domain}Model;
import com.livepan.server.service.model.PageModel;
import com.livepan.server.utils.CopyUtil;
import com.livepan.server.utils.UUIDUtil;
import com.livepan.server.utils.ValidatorUtil;
import com.livepan.server.validator.ValidationResult;
import com.livepan.server.validator.ValidatorImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import sun.nio.cs.CharsetMapping;

<#list typeSet as type>
    <#if type='Date'>
import java.util.Date;
    </#if>
</#list>

import java.util.List;
import java.util.stream.Collectors;

@Service
public class ${Domain}Service {
    @Autowired
    private ${Domain}DOMapper ${domain}DOMapper;

    @Autowired
    private ValidatorImpl validator;

    public void list(PageModel pageModel) {
        PageHelper.startPage(pageModel.getPage(), pageModel.getSize());
        ${Domain}DOExample ${domain}DOExample = new ${Domain}DOExample();
        ${domain}DOExample.setOrderByClause("sort asc");
        List<${Domain}DO> ${domain}DOList = ${domain}DOMapper.selectByExample(${domain}DOExample);
        PageInfo<${Domain}DO> pageInfo = new PageInfo<>(${domain}DOList);
        pageModel.setTotal(pageInfo.getTotal());
        List<${Domain}Model> ${domain}ModelList = ${domain}DOList.stream().map(
                this::convert${Domain}ModelFrom${Domain}DO
        ).collect(Collectors.toList());
        pageModel.setList(${domain}ModelList);
    }

    public void save(${Domain}Model ${domain}Model) throws BusinessException {
        ValidationResult validateResult = validator.validate(${domain}Model);
        if (validateResult.isHasError()) {
            throw new BusinessException(EmBusinessError.PARAMETER_VALIDATION_ERROR, validateResult.getErrMsg());
        }
        ${Domain}DO ${domain}DO = CopyUtil.copy(${domain}Model, ${Domain}DO.class);
        if (${domain}DOMapper.selectByPrimaryKey(${domain}DO.getId()) == null) {
            this.insert(${domain}DO);
        } else {
            this.update(${domain}DO);
        }
    }

    private void insert(${Domain}DO ${domain}DO) {
        <#list typeSet as type>
            <#if type='Date'>
        Date now = new Date();
            </#if>
        </#list>
        ${domain}DO.setId(UUIDUtil.getShortUUID());
        ${domain}DOMapper.insert(${domain}DO);
    }

    private void update(${Domain}DO ${domain}DO) {
        ${domain}DOMapper.updateByPrimaryKey(${domain}DO);
    }

    public void delete(String id){
        ${domain}DOMapper.deleteByPrimaryKey(id);
    }


    private ${Domain}Model convert${Domain}ModelFrom${Domain}DO(${Domain}DO ${domain}DO) {
        if (${domain}DO == null) {
            return null;
        }
        ${Domain}Model ${domain}Model = new ${Domain}Model();
        BeanUtils.copyProperties(${domain}DO, ${domain}Model);
        return ${domain}Model;
    }
}
